package cn.itsource.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.domain.JSONResult;
import cn.itsource.basic.util.SendUtil;
import cn.itsource.domain.org.Shop;
import cn.itsource.domain.org.ShopType;
import cn.itsource.dto.ShopDTO;
import cn.itsource.mapper.EmployeeMapper;
import cn.itsource.mapper.ShopComboMapper;
import cn.itsource.mapper.ShopMapper;
import cn.itsource.mapper.ShopTypeMapper;
import cn.itsource.service.ShopService;
import cn.itsource.util.RedisService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopTypeMapper shopTypeMapper;
    @Autowired
    ShopComboMapper shopComboMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RedisService redisService;

    /****************************************************
    *
    *获取店铺类型
    *
    ****************************************************/
    @Override
    public JSONResult shopTypes() {
        List<ShopType> shopTypes = shopTypeMapper.selectList(null);
        return JSONResult.success(shopTypes);
    }

    /****************************************************
    *
    *
    *获取套餐
    ****************************************************/
    @Override
    public JSONResult combos() {
        return JSONResult.success( shopComboMapper.selectList(null));
    }

    @Override
    public  String autoState(String uuid) {
        //1.校验uuid 是否失效或者是否伪造
        if (!redisService.exists(uuid)) throw new RuntimeException("您的激活时效超时,请重新发送");
        Long shopId = redisService.getKeyObjectValue(uuid, Long.class);
        Shop shop = shopMapper.selectById(shopId);
        shop.setState(2);
        shop.setStatus(Boolean.TRUE);
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//设置起时间
        //System.out.println("111111111::::"+cal.getTime());
        cal.add(Calendar.YEAR, shop.getYear());//增加一年
        shop.setValidTime(cal.getTime());
        shopMapper.updateById(shop);
        return "激活成功,请去登录";
    }
    /****************************************************
     *
     *业务逻辑:
     * 1.参数校验
     * 2.保存数据到数据库,校验店铺名称是否已经存在
     * 3.新增时应该先新增店铺数据,然后返回店铺id,再新增员工信息
     *
     ****************************************************/
    @Override
    public JSONResult register(ShopDTO shopDTO) {
        //1.校验店铺名称是否重复
        if (shopMapper.findNameExist(shopDTO.getShop().getName())) throw  new RuntimeException("店铺名称已存在,请更换");
        //2.先新增店铺
        //生成雪花id
        shopDTO.getShop().setSnowflakeId();
        shopDTO.getShop().setBuyTime(new Date());
        shopMapper.insert(shopDTO.getShop());
        Long shopId = shopDTO.getShop().getId();
        //3.新增管理员员工
        //3.1先将shopIdset进去
        shopDTO.getEmployee().setShopId(shopId);
        //3.2密码加盐加密
        //先设置盐值
        //再将密码与盐值取出来通过md5工具类加盐加密
        //再将加密的密码set进去

        shopDTO.getEmployee().setSnowflakeId();
        shopDTO.getEmployee().setSalt(RandomUtil.randomString(6));
        shopDTO.getEmployee().setPassword(MD5.create().setSalt(shopDTO.getEmployee().getSalt().getBytes()).digestHex(shopDTO.getEmployee().getPassword()));
        shopDTO.getEmployee().setShopAdmin(Boolean.TRUE);
        employeeMapper.insert(shopDTO.getEmployee());
        //需要发送邮箱信息完成激活操作
        // 1.首先给用户发送一哥地址,这个地址一定是我们自己的状态激活的接口,并且需要给一个激活的时间限制 默认30分钟
        String uuid = IdUtil.fastSimpleUUID();
        redisService.setStringKeyAndValue(uuid,shopId,30);
        //创建生产者 将消息丢到mq中
//        EmailMsg emailMsg = new EmailMsg("pethome商家入驻", "Http://localhost/shop/autoState/" + uuid, shopDTO.getEmployee().getEmail());
//        String s = JSON.toJSONString(emailMsg);
//        rabbitTemplate.convertAndSend(
//                RabbitmqConfig.EXCHANGE_TOPICS_INFORM,   //交换机
//                "email.xxxx",
//                s
//        );
        SendUtil.sendEmail("pethome商家入驻","Http://localhost/shop/autoState/"+uuid,shopDTO.getEmployee().getEmail());
        return JSONResult.success();
    }
}
